package com.ruoyi.project.system.average_income_comparison.service.impl;

import java.util.*;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.DataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.average_income_comparison.domain.AverageIncomeInfo;
import com.ruoyi.project.system.average_income_comparison.service.IAverageIncomeInfoService;
import com.ruoyi.project.system.average_income_comparison.mapper.AverageIncomeInfoMapper;

/**
 * 年度人均收入对比Service业务层处理
 *！！！！！注意！！！！！
 * 该模块的domain了busi_income_data表、base_family_info表、base_family_member_info表
 * @author fhk
 * @date 2023-03-07
 */
@Service
public class AverageIncomeInfoImpl implements IAverageIncomeInfoService {
    @Autowired
    private AverageIncomeInfoMapper averageIncomeInfoMapper;

    /**
     * 通过户信息id查询年均收入
     *
     * @param fid 户信息ID
     * @return 年均收入
     */
    @Override
    public AverageIncomeInfo selectAverageIncomeInfoByFid(String fid) {
        return averageIncomeInfoMapper.selectAverageIncomeInfoByFid(fid);
    }

    /**
     * 通过姓名查询年均收入
     *
     * @param name 户主姓名
     * @return 年均收入
     */
    @Override
    public AverageIncomeInfo selectAverageIncomeInfoByName(String name) {
        return averageIncomeInfoMapper.selectAverageIncomeInfoByName(name);
    }

    /**
     * 通过所属年份查询年均收入
     *
     * @param reportYear 所属年份
     * @return 年均收入
     */
    @Override
    public AverageIncomeInfo selectAverageIncomeInfoByYear(Integer reportYear) {
        return averageIncomeInfoMapper.selectAverageIncomeInfoByYear(reportYear);
    }

    /**
     * 通过自然村查询年均收入
     *
     * @param natVillage 户信息ID
     * @return 年均收入
     */
    @Override
    public AverageIncomeInfo selectAverageIncomeInfoByNatVillage(Integer natVillage) {
        return averageIncomeInfoMapper.selectAverageIncomeInfoByNatVillage(natVillage);
    }

    /**
     * 查询年均收入列表
     *
     * @param averageIncomeInfo
     * @return 年均收入
     */
    @Override
    public List<AverageIncomeInfo> selectAverageIncomeInfoList(AverageIncomeInfo averageIncomeInfo) {
        return averageIncomeInfoMapper.selectAverageIncomeInfoList(averageIncomeInfo);
    }

    /**
     * 仿写c++数对，不会写就不要瞎改！
     * @param <L>
     * @param <R>
     */
    private class Pair<L, R>{
        private L left;
        private R right;

        public L getLeft() {
            return left;
        }

        public void setLeft(L left) {
            this.left = left;
        }

        public R getRight() {
            return right;
        }

        public void setRight(R right) {
            this.right = right;
        }

        Pair(L left, R right){
            this.left = left;
            this.right = right;
        }
        Pair(){}
    }

    /**
     * 查找返贫算法
     */
    public List<AverageIncomeInfo> findBackIntoPoverty(AverageIncomeInfo averageIncomeInfo){
        Integer maxYear = averageIncomeInfoMapper.getMaxReportYear();
        //如果数据全部封停，则调用系统时间
        if(maxYear == null){
            Calendar cal = Calendar.getInstance();
            maxYear = cal.get(Calendar.YEAR);
        }
        AverageIncomeInfo tmp = new AverageIncomeInfo(averageIncomeInfo);
        tmp.setReportYear(maxYear);
        List<AverageIncomeInfo> thisYearList = selectAverageIncomeInfoList(tmp);
        tmp.setReportYear(maxYear - 1);
        List<AverageIncomeInfo> lastYearList = selectAverageIncomeInfoList(tmp);
//        if(thisYearList.size()!=lastYearList.size()){
//            throw new DataException("请补全年份数据后再进行比较！");
//        }
        if(thisYearList.size()==lastYearList.size()){
            for(int i=0;i<thisYearList.size();i++){
                if(!Objects.equals(lastYearList.get(i).getFid(), lastYearList.get(i).getFid())){
                    throw new DataException("请补全年份数据后再进行比较！");
                }
            }
        }
        else{
            throw new DataException("请补全年份数据后再进行比较！");
        }
        List<AverageIncomeInfo> result = new ArrayList<>(Collections.emptyList());
        HashMap<String, Pair<AverageIncomeInfo, Double>> hashMap = new HashMap<>();
        //将今年收入传入数对右值
        for(AverageIncomeInfo i:thisYearList){
//            i.setIsBackIntoPoverty(0);
            hashMap.put(i.getFid(), new Pair<>(i, i.getAvgIncome()));
        }
        //将数对右值与去年收入相减并作判断
        for(AverageIncomeInfo i:lastYearList){
            Pair<AverageIncomeInfo, Double> pair = hashMap.get(i.getFid());
            hashMap.remove(i.getFid());
            tmp = pair.left;
            tmp.setLastAvgIncome(i.getAvgIncome());
            if (pair.getRight() < i.getAvgIncome()) {
                tmp.setIsBackIntoPoverty(1);
            }
//            else {
//                tmp.setIsBackIntoPoverty(0);
//            }
            result.add(tmp);
        }
        //如果没有去年的信息，但是有今年的信息，依然传入结果中
        if(!hashMap.isEmpty()){
            for(String key:hashMap.keySet()){
                Pair<AverageIncomeInfo, Double> pair = hashMap.get(key);
                result.add(pair.left);
            }
        }
        //排序，让返贫的始终在上面
        //result.sort((l, r) -> r.getIsBackIntoPoverty().compareTo(l.getIsBackIntoPoverty()));
        result.sort((l, r) -> Objects.equals(l.getIsBackIntoPoverty(), r.getIsBackIntoPoverty()) ? l.getFid().compareTo(r.getFid()) : r.getIsBackIntoPoverty() - l.getIsBackIntoPoverty());
        return result;
    }

}

